package com.ryze.common.dubbo.filter;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;

import java.util.Map;

@Slf4j
@Activate(group = {CommonConstants.PROVIDER})
public class ProviderRpcContextFilter implements Filter {
    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        try {
            Map<String,Object> attachments = RpcContext.getContext().getObjectAttachments();
            RyzeRpcContext.getContext().extractContextParams(attachments);
            log.debug("obtain rpc context:", JSON.toJSONString(RyzeRpcContext.getContext().getContextParams()));
            return invoker.invoke(invocation);
        }finally {
            RyzeRpcContext.removeContext();
        }
    }
}
